#include<bits/stdc++.h>
using namespace std;

int f[1010][1010]; 

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int m, n, x, y;
		cin>>m>>n>>x>>y;
		int ans = 0;
		for(int i = 1;i <= m;i++)
		{
			for(int j = 1;j <= n;j++)
			{
				cin>>f[i][j];
				f[i][j] += f[i-1][j]+f[i][j-1]-f[i-1][j-1];
				if(i >= x && j >= y)
					ans = max(ans, f[i][j]-f[i-x][j]-f[i][j-y]+f[i-x][j-y]);
			}
		}
		cout<<ans<<endl;
	}
	cout<<endl;
	system("pause"); 
	return 0;
}

